<?php

// 信息管理

namespace app\console\controller;

use think\Db;
use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Response;
use think\Controller;
use app\common\trilateral\Classify as ClassifyAlias;
use app\console\model\Information as InformationAlias;

class Information extends Controller
{

    /**
     * 信息列表
     */
    public function index(Request $request, InformationAlias $information)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['class_id'])) $condition['parenTid'] = ['eq', $inquire['class_id']];

        // 读取信息
        $data = $information->with('InformationType')->where($condition)->order(['sort' => 'desc', 'id' => 'desc'])->paginate(10, false, ['query' => $inquire]);



        return $this->fetch('index', [
            'mate_title' => '信息列表',
            'data'       => $data,
            'classify'   => ClassifyAlias::dimension(Cache::get('informationType'))
        ]);
    }


    /**
     * 信息添加
     */
    public function create(Request $request, InformationAlias $information)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Information');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $information->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Information/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('create', [
            "mate_title" => '信息添加',
            'data'       => ClassifyAlias::dimension(Cache::get('informationType')),
        ]);

    }


    /**
     * 信息编辑
     */
    public function update($id, Request $request, InformationAlias $information)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Information');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $information->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('Information/index'));
            }

            fail('操作失败');

        }

        $item = InformationAlias::get($id);


        return $this->fetch('create', [
            "mate_title" => '信息编辑',
            'data'       => ClassifyAlias::dimension(Cache::get('informationType')),
            'item'       => $item

        ]);
    }


    /**
     * 栏目状态改变
     */
    public function auditing(InformationAlias $information)
    {
        $id = Request::instance()->param('id');
        $status = $information->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($information->where('id', $id)->update(['status' => $status])) {

            return $status;
        }
        return false;
    }


    /**
     * 排列顺序
     */
    public function sorting(InformationAlias $information)
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {

            $information->where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('排序成功', Url::build('Information/index'));
    }


    /**
     * 上传组图
     */
    public function uploadArr()
    {
        $file = request()->file('file');
        if (!empty($file)) {

            // 移动到框架应用根目录/public/uploads/ 目录下
            $upload = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($upload) {

                //图片处理
                imgDispose('public' . DS . 'uploads' . DS . $upload->getSaveName());

                return json_encode(['error' => '0', 'src' => 'uploads/' . date('Ymd') . '/' . $upload->getFilename(), 'name' => $upload->getSaveName()]);
            }
            return json_encode(['error' => '失败']);
        }
    }

    /*
    * 信息删除
    */
    public function destroy($id)
    {

        if (InformationAlias::destroy($id)) {
            win('删除成功', Url::build('Information/index'));
        }

        fail('删除失败');

    }
}